
from ui.cli_progress import printProgressBar
from src.utils import *

import argparse

def parse_arguments(rosen):
    parser = argparse.ArgumentParser(description="Rosen: YouTube/Invidious client.",
            formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("-iv",  "--video",          help="play video by url"                                )
    parser.add_argument("-c",   "--channel",        help="display channel videos by url"                    )  
    parser.add_argument("-s",   "--stats",          help="display watched videos stats", action="store_true")
    parser.add_argument("-v",   "--version",        help="display version",              action="store_true")
    parser.add_argument("-pi",  "--plugin",         help="install plugin by name"                           )
    parser.add_argument("-pu",  "--plugun",         help="uninstall plugin by name"                         )
    parser.add_argument("-pp",  "--plugprint",      help="display all plugin names",     action="store_true")
    parser.add_argument("-pua", "--plugupdateall",  help="update all installed plugins", action="store_true")

    for plugin in rosen.plugman.plugins:
        if plugin.flag_action != "":
            parser.add_argument(plugin.flag, plugin.full_flag, help=plugin.flag_help, action=plugin.flag_action)
        else: parser.add_argument(plugin.flag, plugin.full_flag, help=plugin.flag_help)

    args = parser.parse_args()

    rosen.install_default_plugins()

    for arg in vars(args):
        attr = getattr(args, arg)
        if not attr in [None, False]:
            plugin = get_plugin_by_flag(rosen.plugman.plugins, arg)
            if plugin != None: 
                if plugin.is_query_required:
                    rosen.display_query_plugin(plugin=plugin, query=attr)
                else:
                    rosen.display_plugin_items(plugin=plugin, items=plugin.get_items())               


    if args.video != None: rosen.load_video(args.video)
    elif args.channel != None: rosen.display_channel(0, channelUrl=args.channel)
    elif args.stats != False: 
        rosen.print_stats(); exit(0)
    elif args.version != False:
        rosen.print_version(); exit(0)
    elif args.plugin != None:
        print(rosen.plugman.load_from_repo_by_name(args.plugin, pbfunc=printProgressBar))
        exit(0)
    elif args.plugun != None: 
        print(rosen.plugman.remove_plugin(args.plugun, pbfunc=printProgressBar))
        exit(0)
    elif args.plugprint != False:
        print(rosen.plugman.get_repo_plugins()); exit(0)
    elif args.plugupdateall != False:
        rosen.plugman.update_all_user_plugins(pbfunc=printProgressBar); exit(0)


